$(function(){
	
	//	选择日期
	var today = new Date();
	var followDate = new mui.DtPicker({
	    type: "date",//设置日历初始视图模式 
	    beginDate: new Date(1900, 01, 01),//设置开始日期 
	    endDate: new Date(today.getFullYear(),today.getMonth(),today.getDate()),//设置结束日期 
	}); 
	$(".historyTime").click(function(){
	    followDate.show(function(items) {
	    	$(".historyTime").text(items.value).css("color","#333");
	    	$("input[name='diagnosticDate']").val(items.value);
		})  
    });

		
	//	图片上传
	$(".camera-area").fileUpload({
		"upload_url": "/att/upload", //上传url
		"remove_url": "/att/delete", //删除url
		"file": "file",
		"all": false, //是否启用压缩 
		"inputName": "questionfiles", //后台Vo对应的属性,
		"complete": function(response, inputID) {
			if(response.status == 200) {
			   //创建input元素
				var inputElement="<input type='hidden' name='diagnosticFiles' id='"+inputID+"' value='"+response.result.fullurl+"' key='"+response.result.filekey+"'/>";
				$("."+inputID).append(inputElement);
				//隐藏进度条
				$("#shadow_"+inputID).hide();
				var FilesLenght = $("input[name='diagnosticFiles']").length;
				if(FilesLenght==4){
					$(".add").hide();
				}
				console.info("文件路径:"+response.result.fullurl);
				console.info("文件key:"+response.result.filekey);
				
			} else {
				console.error("文件上传失败!");
			}	
		}
	});
	var questionfilesObject=$("input[name='diagnosticFiles']").length;
	var questionfiles=""; 
    $("input[name='diagnosticFiles']").each(function(){
    	questionfiles+=$(this).val()+",";
	});
	$("input[name='diagnosticFiles']").val(questionfiles);
	
//	新增
	$(".submitOrder").on("click",function(){
		var description=$("textarea[name='description']").val();
		if(description==""){
			mui.alert("请输入病情描述!","提示");
			return false;
		}
		if(description.length<10||description.length>=500){
			mui.alert("请输入10~500字!","提示");
			return false;
		}
		
		var diagnosticRecord=$("input[name='diagnosticRecord']").val();
		if(diagnosticRecord==""){
			mui.alert("请输入诊断记录!","提示");
			return false;
		}
		
		var doctorName=$("input[name='doctorName']").val();
		if(doctorName==""){
			mui.alert("请输入医生姓名!","提示");
			return false;
		}
		
		var diagnosticDate=$("input[name='diagnosticDate']").val();
		if(diagnosticDate==""){
			mui.alert("请选择日期!","提示");
			return false;
		}
		
		var id = utilModule.getQueryString("archivesId");
		var historyId = utilModule.getQueryString("id");
		if(historyId==null){
			var param=$("#historyOrder").serialize()+"&archivesId="+id;
			function callBack(data){
				if(data.result==0){
					mui.alert("保存失败!","提示");
					location.reload();
				}else{
					mui.alert("保存成功","提示",function(){
						location.href="medical-history.html?archivesId="+id;
					}); 
				}
			}
			electronicArchivesMdule.insertpage(param, callBack);
		}else{
			var param=$("#historyOrder").serialize()+"&id="+historyId;
			function callBack(data){
				if(data==1){
					mui.alert("修改成功","提示",function(){
						location.href="medical-history.html?archivesId="+id;
					}); 
				}else{
					mui.alert("修改失败!","提示");
					location.reload();
				}
			}
			electronicArchivesMdule.updatepage(param, callBack);
		}
	});
	
	// 修改
	changeupdatepage();
	function changeupdatepage(){
		var historyId = utilModule.getQueryString("id");
		if(historyId!=null){
			var param="pdId="+historyId;
			function callBack(data){
				console.log(data)
				$("textarea[name='description']").val(data.description);
				$("input[name='diagnosticRecord']").val(data.diagnosticRecord);
				$("input[name='doctorName']").val(data.doctorName);
				$("input[name='diagnosticDate']").val(data.diagnosticDate);
				$(".historyTime").text(data.diagnosticDate).css("color","#333");
				var arr = data.diagnosticFiles.split(",");
				var html ="";
				for( i in arr){
					html+= '<li class="'+i+'"><span></span><div><i id="shadow_'+i+'" style="display: none;"></i><img src="'+arr[i]+'"></div><input type="hidden" name="diagnosticFiles" id="'+i+'" value="'+arr[i]+'"></li>'
				}
				$(".thumb").prepend(html);
				var FilesLenght = $("input[name='diagnosticFiles']").length;
				if(FilesLenght==4){
					$(".add").hide();
				}
				$(".thumb li span").on("click",function(){
					$(this).parent().remove();
					$(".add").show();
				});
				
			}
			electronicArchivesMdule.getpage(param, callBack);
		}
	};

});









